package com.vstudy.zx.service.impl;

import com.vstudy.zx.dao.*;
import com.vstudy.zx.entity.Article;
import com.vstudy.zx.entity.Grade;
import com.vstudy.zx.entity.Outline;
import com.vstudy.zx.entity.Subject;
import com.vstudy.zx.service.CatalogService;
import com.vstudy.zx.utils.IOUtil;
import com.vstudy.zx.utils.page.ArticlePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {
    @Autowired
    private ArticleManageDao articleManageDao;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private GradeDao gradeDao;
    @Autowired
    private OutlineDao outlineDao;


    @Override
    public List<Article> getArticlesPage(ArticlePage page, Integer gradeId, Long subjectId, HttpServletRequest request) {
        String basePath = "";
        try {
            basePath = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<Article> articles = articleManageDao.selectPage(page, null, gradeId, subjectId); // 自动返回page.totalCount
        for (Article a :
                articles) {
            String path = a.getArticle_content();

            path = basePath + "/static" + path;

            try {
                String content = IOUtil.readArticle(path, "utf-8");
                a.setArticle_content(content);
            } catch (IOException e) {
                e.printStackTrace();
            }

            a.releaseTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(a.getArticle_release_time());
        }
        return articles;
    }

    @Override
    public Subject getSubjectById(Long sId) {
        return subjectDao.selectById(sId);
    }

    @Override
    public Grade getGradeById(Integer gId) {
        return gradeDao.selectById(gId);
    }

    @Override
    public List<Article> getArticlesByOutlineId(Long outlineId, HttpServletRequest request) {
        String basePath = "";
        try {
            basePath = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        List<Article> articles = articleDao.selectByOutlineId(outlineId);
        for (Article a :
                articles) {
            String path = a.getArticle_content();
            path = basePath + "/static" + path;

            try {
                String content = IOUtil.readArticle(path, "utf-8");
                a.setArticle_content(content);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return articles;
    }

    @Override
    public Outline getOutlineById(Long outlineId) {
        return outlineDao.selectById(outlineId);
    }

    @Override
    public List<Article> getArticlesPage(ArticlePage page, Long oid, HttpServletRequest request) {
        List<Article> articles = articleManageDao.selectPageByOid(page, oid); // 自动返回page.totalCount
        for (Article a :
                articles) {
            String path = a.getArticle_content();
            path = request.getServletContext().getRealPath(path);

            try {
                String content = IOUtil.readArticle(path, "utf-8");
                a.setArticle_content(content);
            } catch (IOException e) {
                e.printStackTrace();
            }

            a.releaseTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(a.getArticle_release_time());
        }
        return articles;
    }

    // 根据subject 的Id获取大纲
    public List<Outline> getOutlinesBySubjectId(Long subjectId) {
        List<Outline> outlines = outlineDao.selectBySubjectId(subjectId);
        return outlines;
    }
}
